package com.eightywork.life.activity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.text.Html;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import cn.jpush.android.api.JPushInterface;

import com.eightywork.life.R;
import com.eightywork.life.service.DoService;
import com.eightywork.life.util.MyHttpException;
import com.umeng.analytics.MobclickAgent;

public class PasswordSecondActivity extends BaseActivity implements
		OnClickListener {
	private EditText secode, password1, password2;
	private ImageButton returnButton;
	private Button saveButton, showButton;
	private AsyncTask task;
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_password2);
		initView();
	}

	@Override
	protected void onDestroy() {
		if (task != null) {
			task.cancel(true);
		}
		super.onDestroy();
	}
	
	@Override
	public void onResume() {		
		super.onResume();
		JPushInterface.onResume(this);
	    MobclickAgent.onResume(this);
	}
	
	@Override
	public void onPause() {
	    super.onPause();
		JPushInterface.onPause(this);
	    MobclickAgent.onPause(this);
	}
	public void initView() {
		secode = (EditText) findViewById(R.id.secode);
		password1 = (EditText) findViewById(R.id.password1);
		password2 = (EditText) findViewById(R.id.password2);
		returnButton = (ImageButton) findViewById(R.id.button_left);
		saveButton = (Button) findViewById(R.id.button_save);
		showButton = (Button) findViewById(R.id.button_show);
		returnButton.setOnClickListener(this);
		saveButton.setOnClickListener(this);
		showButton.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v == returnButton) {
			finish();
		} else if (v == saveButton) {
			save();
		} else if (v == showButton) {
//			password1.setInputType(InputType.TYPE_CLASS_TEXT);
//			password2.setInputType(InputType.TYPE_CLASS_TEXT);
//			password1.getInputType()
			if (showButton.getText().equals("显示密码")) {
				showButton.setText("隐藏密码");
				password1.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
				password2.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
			} else {
				showButton.setText("显示密码");
				password1.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
				password2.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
			}
		}
	}

	private void save() {
		String username = getIntent().getStringExtra("username");
		String uid = getIntent().getStringExtra("uid");
		String secodeString = secode.getEditableText().toString();
		String password1String = password1.getEditableText().toString();
		String password2String = password2.getEditableText().toString();
		if (secodeString.equals("")) {
			secode.setError(Html.fromHtml("<font color=red>请填写验证码</font>"));
			return;
		}
		if (password1String.equals("")) {
			password1.setError(Html.fromHtml("<font color=red>请填写新密码</font>"));
			return;
		}
		if (!password1String.equals(password2String)) {
			password2.setError(Html.fromHtml("<font color=red>必须和新密码相同</font>"));
			return;
		}
		task = new SaveTask().execute(username, uid, password1String,
				password2String, secodeString);
	}

	class SaveTask extends AsyncTask<String, String, String> {

		@Override
		protected void onPreExecute() {
			showProgressDialog("请稍后...");
			super.onPreExecute();
		}

		@Override
		protected String doInBackground(String... params) {
			try {
				DoService.lostpasswd2(params[0], params[1], params[2],
						params[3],params[4]);
			} catch (MyHttpException e) {
				return e.getErrorMessage();
			}
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			hideProgressDialog();
			if (result == null) {
				finish();
			} else {
				showMessage(result);
			}
			super.onPostExecute(result);
		}
	}
}
